package com.qilu.pe.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.EventModel;
import com.qilu.pe.support.util.AlertUtil;
import com.ruitu.arad.Arad;
import com.ruitu.arad.util.AppUtils;
import com.ruitu.arad.util.ToastUtils;

import androidx.appcompat.app.AlertDialog;

// 设置页面
public class SettingActivity extends Base2Activity {
    // 退出登录 修改密码 意见反馈 更换手机
    private TextView tv_log_out, tv_change_pwd, tv_feedback, tv_change_phone;
    // 联系我们 清除缓存 当前版本
    private TextView tv_contact_us, tv_cache, tv_version;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_setting;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tv_log_out = findViewById(R.id.tv_log_out);
        tv_change_pwd = findViewById(R.id.tv_change_pwd);
        tv_feedback = findViewById(R.id.tv_feedback);
        tv_change_phone = findViewById(R.id.tv_change_phone);
        tv_contact_us = findViewById(R.id.tv_contact_us);
        tv_cache = findViewById(R.id.tv_cache);
        tv_version = findViewById(R.id.tv_version);

        setHeadTitle("设置");

        setListeners();

        alert = new AlertDialog.Builder(this, R.style.Dialog_FS).create();

        tv_version.setText("v" + AppUtils.getAppVersionName());
    }

    private void setListeners() {
        setOnClickListener(tv_log_out, tv_change_pwd, tv_feedback, tv_change_phone, tv_contact_us
                , tv_cache, tv_version);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_log_out) {// 退出登录
            showDialog();
        }
        if (v == tv_change_pwd) {// 修改密码
            startActivity(ChangePwdActivity.class);
        }
        if (v == tv_feedback) {// 意见反馈
            startActivity(FeedBackActivity.class);
        }
        if (v == tv_change_phone) {// 更换手机
            startActivity(ChangePhoneActivity.class);
        }
        if (v == tv_contact_us) {// 联系我们

        }
        if (v == tv_cache) {// 清除缓存
            showProgressWithText(true, "清理中..");
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(600);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            hideProgress();
                            ToastUtils.showShort("已清理");
                        }
                    });
                }
            }).start();
        }
        if (v == tv_version) {// 当前版本
//            ToastUtils.showShort("当前本版:v" + AppUtils.getAppVersionName());
        }
    }

    private AlertDialog alert;// 弹窗
    private View alert_view;

    public void showDialog() {// 弹窗
        alert.show();
        alert_view = View.inflate(this, R.layout.layout_alert_com_alert, null);
        alert_view.findViewById(R.id.tv_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert.dismiss();
            }
        });
        alert_view.findViewById(R.id.tv_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Global.HEADER = "";
                alert.dismiss();
                startActivity(LoginActivity.class);
                Arad.bus.post(new EventModel(20101651));
                finish();
            }
        });

        AlertUtil.setAlertCenterStyle(alert.getWindow(), alert_view, this);
    }
}